package com.yunji.web.base;

import com.yunji.aggregate.model.dto.base.SiteParam;
import com.yunji.aggregate.model.vo.base.SiteStatisticsVO;
import com.yunji.aggregate.service.BaseAggregateService;
import com.yunji.base.business.SiteBusiness;
import com.yunji.base.dto.ChangeBindDTO;
import com.yunji.base.dto.SiteDTO;
import com.yunji.base.dto.SiteStaffDTO;
import com.yunji.base.vo.SiteInfoVO;
import com.yunji.common.annotation.Log;
import com.yunji.common.base.BaseController;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.common.enums.common.BusinessType;
import com.yunji.common.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @version 1.0
 * @Project: yunji-coffee
 * @Package com.yunji.web.site
 * @FileName SiteController.java
 * @Copyright Copyright © 2024 云集互联. All Rights Reserved
 * Company		com
 * @CreateTime 2024-04-02 14:54:23
 * @Description 网点管理
 * @Modification Author                    History Time			Version				Description
 * --------------------------------------------------------------------------------
 * HuaAo0803@163.com	2024-05-28 14:54:23		1.0					1.0 Version
 * @since JDK 1.8.0_202
 */
@RestController
@Api(tags = "管理端-网点管理")
@RequestMapping("/container/site")
public class SiteController extends BaseController {
    @Resource
    private BaseAggregateService baseAggregate;

    @Resource
    private SiteBusiness business;

    @ApiOperation("查询网点列表")
//    @PreAuthorize("@ss.hasPermi('container:site:query')")
    @GetMapping("/list")
    public Response<List<SiteStatisticsVO>> list(@Valid SiteParam param) {
        List<SiteStatisticsVO> list = baseAggregate.siteList(param);
        return ResponseHelper.successWithPage(list);
    }

    @ApiOperation("导出网点列表")
    @Log(title = "网点", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('container:site:export')")
    @PostMapping("/export")
    public void export(SiteParam param) {
        param.setExport(true);
        List<SiteStatisticsVO> list = baseAggregate.siteList(param);
        ExcelUtil<SiteStatisticsVO> util = new ExcelUtil<>(SiteStatisticsVO.class);
        util.exportExcel(response, list, "网点数据");
    }

    @ApiOperation("获取网点详细信息")
    @GetMapping(value = "/{id}")
    public Response<SiteInfoVO> getInfo(@PathVariable("id") Long id) {
        return ResponseHelper.success(business.getSiteInfoVO(id));
    }

    @ApiOperation("新增网点")
    @Log(title = "网点", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('container:site:add')")
    @PostMapping
    public Response<Long> create(@RequestBody SiteDTO dto) {
        return ResponseHelper.success(business.create(dto));
    }

    @ApiOperation("修改网点")
    @Log(title = "网点", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('container:site:edit')")
    @PutMapping
    public Response<String> update(@RequestBody SiteDTO dto) {
        business.update(dto);
        return ResponseHelper.success();
    }

    @ApiOperation("删除网点")
    @Log(title = "网点", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('container:site:remove')")
    @DeleteMapping("/{ids}")
    public Response<String> remove(@PathVariable Long... ids) {
        business.delete(ids);
        return ResponseHelper.success();
    }

    @ApiOperation("绑定站点员工")
    @PostMapping("/bindSiteStaff")
    public Response<String> bindSiteStaff(@RequestBody SiteStaffDTO dto) {
        business.bindStaffSite(dto);
        return ResponseHelper.success();
    }

    @ApiOperation("网点定价")
    @Log(title = "网点", businessType = BusinessType.UPDATE)
    @PostMapping("/customPrice")
    public Response<String> customPrice(@RequestBody SiteDTO dto) {
        business.customPrice(dto);
        return ResponseHelper.success();
    }

    @ApiOperation("网点换绑")
    @PostMapping("/changeBind")
    @PreAuthorize("@ss.hasPermi('container:site:agent')")
    public Response<String> changeBind(@RequestBody ChangeBindDTO dto) {
        business.changeBind(dto);
        return ResponseHelper.success("换绑成功");
    }

    /*-------------------------------搜索栏---------------------------------*/
    @ApiOperation("网点名称匹配搜索")
    @GetMapping("/searchName")
    public Response<List<String>> searchName(@RequestParam("name") String name) {
        return ResponseHelper.successWithPage(business.searchName(name));
    }

    @ApiOperation("网点电话匹配搜索")
    @GetMapping("/searchPhone")
    public Response<List<String>> searchPhone(@RequestParam("phone") String phone) {
        return ResponseHelper.successWithPage(business.searchPhone(phone));
    }
}
